package org.tcshare.logutils;

import android.os.Environment;
import android.os.Handler;
import android.os.HandlerThread;

import com.orhanobut.logger.AndroidLogAdapter;
import com.orhanobut.logger.Logger;
import com.orhanobut.logger.PrettyFormatStrategy;

import java.io.File;

public class MyAndroidLogAdapter {
    private static final int MAX_BYTES = 5_000 * 1024; // 500K averages to a 4000 lines per file
    private static MyAndroidLogAdapter instance;

    private MyAndroidLogAdapter() {
        String diskPath = Environment.getExternalStorageDirectory().getAbsolutePath();
        String folder = diskPath + File.separatorChar  + "logger";

        HandlerThread ht = new HandlerThread("AndroidFileLogger." + folder);
        ht.start();
        Handler handler = new MyDiskLogStrategy.WriteHandler(ht.getLooper(), folder, MAX_BYTES);
        MyDiskLogStrategy logStrategy = new MyDiskLogStrategy(handler);
        PrettyFormatStrategy formatStrategy = PrettyFormatStrategy.newBuilder().logStrategy(logStrategy).build();

        Logger.addLogAdapter(new AndroidLogAdapter(formatStrategy));
    }

    public static MyAndroidLogAdapter getInstance() {
        synchronized (MyAndroidLogAdapter.class) {
            if (instance == null) {
                instance = new MyAndroidLogAdapter();
            }
        }
        return instance;
    }


}
